package com.bclz.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bclz.bo.CommentBo;
import com.bclz.bo.PageBo;
import com.bclz.common.response.ResponseData;
import com.bclz.common.response.ResponseUtils;
import com.bclz.entity.Comment;
import com.bclz.service.CommentService;
import com.bclz.util.EntityHelper;
import com.github.pagehelper.PageInfo;

import ch.qos.logback.classic.Logger;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
* Title: CommentController  
* Description:  评论Handle 
* @author xuchang  
* @date 2018年8月10日
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
	
	private Logger log=(Logger)LoggerFactory.getLogger(getClass());
	
	@Autowired
	private CommentService commentService;
	
	/**
	 * 
	 * Title: selflistResponse  
	 * Description:  贴子id查询评论列表
	 * @param postId
	 * @param page
	 * @return
	 */
	@ApiOperation(value="查询评论列表", notes="根据贴子id查询评论列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "postId", value = "帖子Id", required = true, dataType = "String"),
		@ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "pageSize", value = "每页的数量", required = false, dataType = "Integer")}
	)
	@RequestMapping(value="/list/{postId}",method=RequestMethod.GET)
	public ResponseData selflistResponse(@PathVariable(value="postId") String postId,PageBo page) {
		PageInfo<CommentBo> commentList=commentService.listbyPostId(postId, page);
		return ResponseUtils.success("查询成功", commentList);
	}
	
	@ApiOperation(value="发表评论", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "content", value = "评论内容", required = true, dataType = "String"),
		@ApiImplicitParam(name = "postId", value = "帖子Id", required = true, dataType = "String"),
		@ApiImplicitParam(name = "userId", value = "发表人Id", required = true, dataType = "String")}
	)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public ResponseData sendComment(Comment co) {
		ResponseData validataRes=EntityHelper.validateComment(co);
		if(validataRes.getCode()==0) {
			return validataRes;
		}
		try {
			co=(Comment)validataRes.getData();
			return commentService.add(co)==1?ResponseUtils.success("保存成功", null):ResponseUtils.fail("保存失败", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("评论保存失败{}",e);
			return ResponseUtils.fail("保存失败", null);
		}
		
	}
	
}
